package me.zhengjie.pm.projectbase.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.pm.projectbase.domain.ProjectBase;
import me.zhengjie.pm.projectbase.service.ProjectBaseService;
import me.zhengjie.pm.projectbase.service.dto.ProjectBaseQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author ZGZ
* @date 2020-04-14
*/
@Api(tags = "项目基础信息管理")
@RestController
@RequestMapping("/api/projectBase")
public class ProjectBaseController {

    private final ProjectBaseService projectBaseService;

    public ProjectBaseController(ProjectBaseService projectBaseService) {
        this.projectBaseService = projectBaseService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('projectBase:list')")
    public void download(HttpServletResponse response, ProjectBaseQueryCriteria criteria) throws IOException {
        projectBaseService.download(projectBaseService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询项目基础信息")
    @ApiOperation("查询项目基础信息")
    @PreAuthorize("@el.check('projectBase:list')")
    public ResponseEntity<Object> getProjectBases(ProjectBaseQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(projectBaseService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增项目基础信息")
    @ApiOperation("新增项目基础信息")
    @PreAuthorize("@el.check('projectBase:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody ProjectBase resources){
        return new ResponseEntity<>(projectBaseService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改项目基础信息")
    @ApiOperation("修改项目基础信息")
    @PreAuthorize("@el.check('projectBase:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody ProjectBase resources){
        System.out.println(resources);
        //进行修改的比较
        projectBaseService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除项目基础信息")
    @ApiOperation("删除项目基础信息")
    @PreAuthorize("@el.check('projectBase:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        projectBaseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}